// pages/card/edit.js
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        logo: '../../images/upload_icon.png',
        id: "",
        headpic: '',
        name: '',
        job: '',
        company: '',
        address: "",
        description: "",
        phone:'',
        is_default: false,
    },

    chooseLogo() {
        wx.chooseImage({
            count: 1,
            sizeType: 'compressed',
            success: (res) => {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                wx.showLoading({
                    title: '上传中•••',
                })
                let tempFilePaths = res.tempFilePaths[0];
                wx.uploadFile({
                    url: app.setConfig.url + '/index.php/Asset/Upload/advpPlupload',
                    filePath: tempFilePaths,
                    name: 'file',
                    formData: {
                        'token': app.globalData.token,
                    },
                    success: (res) => {
                        wx.showToast({
                            title: '上传成功',
                            icon: 'success',
                            duration: 800
                        })
                        let data = JSON.parse(res.data).file_url;
                        let src = {srcbd: tempFilePaths, src: data}
                        this.setData({
                            logo: src.srcbd,
                            headpic: src.src
                        })
                    }
                })
            }
        })
    },

    inputChange(e) {
        let id = e.currentTarget.id;
        console.log(e.detail.value)
        this.data[id] = e.detail.value;
        console.log(this.data)
    },

    defaultChange(e) {
        this.data.is_default = e.detail.value ? 1 : 0;
    },

    addClick() {
        if (this.checkForm()) {
          let data = this.data;
          data.token = app.globalData.token;
            app.postLogin(app.setConfig.url + '/index.php/Api/Card/editCard',data , (res) => {
                wx.showToast({
                    title: "保存成功",
                    duration: 1000
                })
                setTimeout(() => {
                    wx.navigateBack()
                }, 1000)
            })
        }
    },
    checkForm() {
        if (!this.data.headpic) {
            wx.showModal({
                content: "请上传名片头像",
                showCancel: false
            })
            return false;
        }
        if (!this.data.name) {
            wx.showModal({
                content: "请填写名片名称",
                showCancel: false
            })
            return false;
        }
        if (!this.data.job) {
            wx.showModal({
                content: "请填写职位",
                showCancel: false
            })
            return false;
        }
        if (!this.data.phone ) {
            wx.showModal({
                content: "请填写手机",
                showCancel: false
            })
            return false;
        }
        if (!this.data.company) {
            wx.showModal({
                content: "请填写公司",
                showCancel: false
            })
            return false;
        }
        if (!this.data.address) {
            wx.showModal({
                content: "请填写地址",
                showCancel: false
            })
            return false;
        }
        if (!this.data.description) {
            wx.showModal({
                content: "请填写简介",
                showCancel: false
            })
            return false;
        }
        return true;
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.id) {
            this.data.id = options.id;
            this.setData(this.data)
        }


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})